//
//  YSProdDetailCommentView.m
//  Yueshi
//
//  Created by Northwan on 15/9/25.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSProdDetailCommentView.h"
#import "YSPhotosView.h"
#import "YSLevelView.h"


@interface YSProdDetailCommentView()
@property (strong, nonatomic) UIImageView *avatarView;
@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *levelLabel;
@property (strong, nonatomic) YSLevelView *levelView;
@property (strong, nonatomic) UILabel *timeLabel;
@property (strong, nonatomic) UILabel *commentLabel;
@property (strong, nonatomic) YSPhotosView *photosView;
@property (strong, nonatomic) UIButton *catMorebtn;
@end

@implementation YSProdDetailCommentView

-(instancetype)initWithComment:(NSArray *)comment{
    if(self = [super init]){
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(YSPublicXInset, 0, SCREENW-YSPublicXInset*2, 0.5)];
        [lineView setBackgroundColor:HEXColor(@"#EAEAE9")];
        [self addSubview:lineView];
        
        YSProdComment *prodComment = comment[0];
        UIImageView *avatarView = [[UIImageView alloc]init];
        [avatarView setImageURLStr:prodComment.userImg];
        [avatarView setFrame:CGRectMake(YSPublicXInset, CGRectGetMaxY(lineView.frame)+YSPublicXInset, 35, 35)];
        [avatarView setCornerRadius:35/2 borderWidth:0 borderColor:[UIColor clearColor]];
        [self addSubview:avatarView];
        _avatarView = avatarView;
        
        UILabel *nameLabel = [[UILabel alloc]init];
        [nameLabel setFont:SYS_FONT(15)];
        [nameLabel setTextColor:publicTextColor];
        CGSize nameSize = [prodComment.userName sizeWithFont:SYS_FONT(15)];
        [nameLabel setText:prodComment.userName];
        [nameLabel setFrame:CGRectMake(CGRectGetMaxX(self.avatarView.frame)+YSPublicXInset, CGRectGetMaxY(lineView.frame)+YSPublicXInset, nameSize.width, 15)];
        [self addSubview:nameLabel];
        _nameLabel = nameLabel;
        
        UILabel *timeLabel = [[UILabel alloc]init];
        [timeLabel setTextColor:HEXColor(@"#9e9e9e")];
        [timeLabel setFont:SYS_FONT(13)];
        CGSize timeSize = [prodComment.createTime sizeWithFont:SYS_FONT(13)];
        timeLabel.frame = CGRectMake(SCREENW-timeSize.width-YSPublicXInset, CGRectGetMaxY(avatarView.frame)-avatarView.frame.size.height/2, timeSize.width, timeSize.height);
        [timeLabel setText:prodComment.createTime];
        [self addSubview:timeLabel];
        _timeLabel = timeLabel;
        
        NSString *levelText = @"好评度";
        CGSize levelTextSize = [levelText sizeWithFont:SYS_FONT(12)];
        UILabel *levelLabel = [[UILabel alloc]init];
        [levelLabel setFont:SYS_FONT(12)];
        [levelLabel setTextColor:HEXColor(@"#b9b9b9")];
        [levelLabel setText:levelText];
        [levelLabel setFrame:CGRectMake(CGRectGetMinX(nameLabel.frame), CGRectGetMaxY(avatarView.frame)-12, levelTextSize.width, 12)];
        [self addSubview:levelLabel];
        _levelLabel = levelLabel;
        
        YSLevelView *levelView = [[YSLevelView alloc]initWithSourceType:DetailSourceType];
        [levelView setX:CGRectGetMaxX(levelLabel.frame)];
        [levelView setY:CGRectGetMinY(levelLabel.frame)];
        [levelView setLevel:prodComment.score];
        levelView.userInteractionEnabled = NO;
        [self addSubview:levelView];
        _levelView = levelView;
        
        UILabel *commentLabel = [[UILabel alloc]init];
        [commentLabel setText:prodComment.comment];
        [commentLabel setFont:SYS_FONT(13)];
        [commentLabel setNumberOfLines:0];
        [commentLabel setTextColor:HEXColor(@"#9e9e9e")];
        CGSize commentTextSize =  [prodComment.comment sizeWithFont:SYS_FONT(13) constrainedToSize:CGSizeMake(SCREENW-YSPublicXInset*2, MAXFLOAT)];
        [commentLabel setFrame:CGRectMake(YSPublicXInset, CGRectGetMaxY(avatarView.frame)+YSPublicXInset, commentTextSize.width, commentTextSize.height)];
        [self addSubview:commentLabel];
        _commentLabel = commentLabel;
        
        CGFloat catMoreBtnY = CGRectGetMaxY(commentLabel.frame);
        
        if(prodComment.commentImage.count){
            YSPhotosView *photosView = [[YSPhotosView alloc]init];
            photosView.isShowProgress = NO;
            [photosView addPhotos:prodComment.commentImage];
            photosView.x = 0;
            photosView.y = CGRectGetMaxY(commentLabel.frame)+YSPublicXInset;
            [self addSubview:photosView];
            _photosView = photosView;
            
            catMoreBtnY = CGRectGetMaxY(photosView.frame);
        }
        
        if(comment.count > 1){
            UIButton *catMoreBtn = [[UIButton alloc]init];
            [catMoreBtn setBackgroundColor:[UIColor clearColor]];
            [catMoreBtn setTitle:@"查看更多" forState:UIControlStateNormal];
            [catMoreBtn.titleLabel setFont:SYS_FONT(14)];
            [catMoreBtn setTitleColor:HEXColor(@"#9e9e9e") forState:UIControlStateNormal];
            [catMoreBtn setFrame:CGRectMake(0, catMoreBtnY+5, SCREENW, 34)];
            [catMoreBtn addTarget:self action:@selector(tapMore:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:catMoreBtn];
            _catMorebtn = catMoreBtn;
            
            catMoreBtnY = CGRectGetMaxY(catMoreBtn.frame);
        }
        self.frame = CGRectMake(0, 0, SCREENW, catMoreBtnY);
    }
    return self;
}

-(void)tapMore:(UIButton *)sender{
    [[NSNotificationCenter defaultCenter]postNotificationName:kOpenProductReviewsDetailNortif object:nil];
}


@end
